*HTML Attributes*



HTML attributes provide additional information about an element, allowing developers to control its behavior, appearance, and interaction. Attributes are always included within the opening tag of an element and are written as name-value pairs.


Basic Example/Syntax





Global Attributes

Global attributes are applicable to almost all HTML elements.

Common Global Attributes

  • `class` – Assigns a class name for CSS styling or JavaScript manipulation.
  • `id` – Provides a unique identifier for an element.
  • `style` – Defines inline CSS styles.
  • `title` – Specifies extra information (appears as a tooltip on hover).
  • `lang` – Declares the language of the element’s content.
  • `dir` – Specifies text direction (`ltr`, `rtl`).

  • The id and class


    The href Attribute (For Links)

    Used in anchor (<a>) tags to define the URL destination of a hyperlink.






    Best Practices

  • Use absolute URLs for external links and relative URLs for internal navigation.
  • Add `target="_blank"` to open links in a new tab.
  • Use `rel="noopener noreferrer"` for security when opening new tabs.



  • The `src` Attribute (For Media Elements)

    The src attribute specifies the source URL for media elements like <img>, <audio>, and <video>.








    Best Practices

  • Always provide an alt attribute for images for accessibility.
  • Use relative URLs for local resources.
  • Optimize media files for faster loading times.
  • Attribute 'style'


    Understanding HTML attributes is essential for writing clean, efficient, and accessible code. By using attributes correctly, developers can enhance functionality, improve user experience, and optimize accessibility and SEO.